361B - Levko and Permutation - CodeForces Solution


constructive algorithms math number theory *1200

Please click on ads to support us..

C++ Code:

                                                     /*JODD or WOTT!*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define repr(i,k) for(int i=k;i>=0;i--)
#define PI 3.1415926535897932384626433832795
#define EPS 1e-9
#define PB push_back
#define MOD 100000007
#define MAX INT_MAX
#define MIN INT_MIN
const int MAXL= (int)1e5+10;
class Graph
{
    public:
    map<int, bool> visited;
    map<int, list<int> > adj;
    void addEdge(int v, int w);
    
    void DFS(int v,vector<int> &ans);
    
};
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
}
 
void Graph::DFS(int v, vector<int> &ans)
{
    visited[v] = true;
    
    ans.push_back(v);
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
    {   
        if (!visited[*i])
            DFS(*i,ans);
    }
        
}
int fact[MAXL];
class factorial
{
    public:
     
    int factL(int x);
};
int factL(int x)
{   
    fact[0]=fact[1]=1;
    if(fact[x]==0)
    {
        rep(i,2,MAXL)
      fact[i]=(fact[i-1]*i)%MOD;
    }
    return fact[x];
}
int gcd(int a, int b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;
    if (a == b)
        return a;
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int lcm(int x,int y)
{   
    return x*y/(gcd(x,y));
}

void run_case()
{
   int n,k;
   cin>>n>>k;
   vector<int>a(n);
   if(k<0 || k>=n){cout<<"-1"<<endl;return;}

    if(k==0)
    {
       a[0]=n;
       rep(i,1,n-1)a[i]=i;
    }
   else if(k==1 )
   {if(n%2){cout<<"-1"<<endl;return;}
   
       a[0]=1;
      if(n>1) a[n-1]=2;
      rep(i,1,n-2)
        a[i]=(i+2);
 
   }
   else if(k==n-1)
   { 
      rep(i,0,n-1)
        a[i]=i+1;
   }
   else
   { 
       a[0]=2+k;
       a[n-1]=1;
      rep(i,1,k)
         a[i]=i+1;
      rep(i,1+k,n-2)
         a[i]=i+2;
   }
   rep(i,0,n-1)
   cout<<a[i]<<" ";
 
}


signed main()
{

ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t=1;//cin>>t;

   while(t--)
   {
       run_case();
   }
return 0;
}


Comments

Submit
0 Comments
More Questions

318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins